import { defineSource } from '../source';
import { ProcessedItem } from '../types'
import { myFetch } from "../utils/fetch"
import logger from '../../framework/config/logger';

export default defineSource({
    ifeng: async (): Promise<ProcessedItem[]> => {
        const html: string = await myFetch("https://www.ifeng.com/")
        const regex = /var\s+allData\s*=\s*(\{[\s\S]*?\});/
        const match = regex.exec(html)
        if (match) {
            const realData = JSON.parse(match[1])
            const rawNews = realData.hotNews1 as {
                url: string
                title: string
                newsTime: string
            }[]
            return rawNews.map((hotNews): ProcessedItem => ({
                id: hotNews.url,
                title: hotNews.title,
                url: hotNews.url,
            }))
        }
        return Promise.resolve([] as ProcessedItem[]);
    }
});